/************************************************************************************/
/*  Program name CAPGLIMP_BC_PACKETS.sql                                        	*/
/*  Creater:  NIP Karen (PCCW - UniHub)                                         	*/
/*************   History   *************************                                */
/* 24 JAN, 2006   NIP Karen         First created                                   */
/************************************************************************************/
SET SERVEROUTPUT ON SIZE 1000000;

ALTER TRIGGER IGI_CEC_GL_BC_PACKETS_T2 DISABLE;

DECLARE
	lv_Prog_ID		VARCHAR2(100)	:=	'CAPGLIMP_BC_PACKETS';

	lv_Scc_Count	NUMBER	:=	0;
	lv_Err_Count	NUMBER	:=	0;
	lv_Err_Msg		VARCHAR2(500)	:=	'No Error';

	CURSOR Cur_F IS
	SELECT
		*
	FROM
		CRST_GL_BC_PACKETS;
	
	lv_Next_Seq_ID		NUMBER;
	lv_COAID			NUMBER;
	
BEGIN
	FOR C1 IN Cur_F LOOP
		BEGIN
			lv_COAID	:=	CAPBRIMP_COMMON.GetCOAID(C1.SOB_SHORT_NAME);
			
			SELECT	GL_BC_PACKETS_S.NextVal
			INTO	lv_Next_Seq_ID
			FROM	DUAL;

			INSERT INTO GL_BC_PACKETS
			VALUES
			(
			lv_Next_Seq_ID,
			CAPBRIMP_COMMON.GetSOBID(C1.SOB_SHORT_NAME),
			C1.JE_Source_Name,
			C1.JE_Category_Name,
			CAPBRIMP_COMMON.GetSegment(lv_COAID,C1.CODE_COMB_ID_D),
			C1.Actual_Flag,
			C1.Period_Name,
			C1.Period_Year,
			C1.Period_Num,
			C1.Quarter_Num,
			C1.Currency_Code,
			C1.Status_Code,
			C1.Last_Update_Date,
			CAPBRIMP_COMMON.GetUser(C1.Last_Updated_Name),
			C1.Budget_Version_ID,
			CAPBRIMP_COMMON.GetEncumTypeID(C1.ENCUM_TYPE_NAME),
			C1.Template_ID,
			C1.Entered_DR,
			C1.Entered_CR,
			C1.Accounted_DR,
			C1.Accounted_CR,
			C1.USSGL_Transaction_Code,
			C1.Originating_RowID,
			C1.Account_Segment_Value,
			C1.Automatic_Encumbrance_Flag,
			CAPBRIMP_COMMON.GetBudgetVerID(C1.FUND_BUDGET_NAME),
			C1.Funds_Check_Level_Code,
			C1.Amount_Type,
			C1.Boundary_Code,
			C1.Tolerance_Percentage,
			C1.Tolerance_Amount,
			C1.Override_Amount,
			C1.DR_CR_Code,
			C1.Account_Type,
			C1.Account_Category_Code,
			C1.Effect_On_Funds_Code,
			C1.Result_Code,
			C1.Budget_Posted_Balance,
			C1.Actual_Posted_Balance,
			C1.Encumbrance_Posted_Balance,
			C1.Budget_Approved_Balance,
			C1.Actual_Approved_Balance,
			C1.Encumbrance_Approved_Balance,
			C1.Budget_Pending_Balance,
			C1.Actual_Pending_Balance,
			C1.Encumbrance_Pending_Balance,
			C1.Reference1,
			C1.Reference2,
			C1.Reference3,
			C1.Reference4,
			C1.Reference5,
			C1.JE_Batch_Name,
			CAPBRIMP_MAPPING.GetJEBatchIDMapping(C1.JE_Batch_ID),
			CAPBRIMP_MAPPING.GetJEHeaderIDMapping(C1.JE_Header_ID),
			C1.JE_Line_Num,
			C1.JE_Line_Description,
			C1.Reference6,
			C1.Reference7,
			C1.Reference8,
			C1.Reference9,
			C1.Reference10,
			C1.Reference11,
			C1.Reference12,
			C1.Reference13,
			C1.Reference14,
			C1.Reference15
			);
		EXCEPTION
			WHEN OTHERS THEN
				DBMS_OUTPUT.Put_Line(SQLERRM);
				lv_Err_Count	:=	lv_Err_Count + 1;
		END;
		lv_Scc_Count	:=	lv_Scc_Count	+	1;
	END LOOP;
	
	IF	lv_Err_Count	>	0	THEN
		ROLLBACK;
	END IF;
	
	INSERT INTO CAPCNV_LOG(
		PROG_ID,
		PROCESS_DATE,
		RECORD_PROCESSED,
		RECORD_REJECTED,
		RECORD_EXTRACTED,
		ERR_MSG
	) VALUES (
		lv_Prog_ID,
		SYSDATE,
		lv_Scc_Count,
		lv_Err_Count,
		lv_Scc_Count-lv_Err_Count,
		lv_Err_Msg
	);
	
	COMMIT;
END;
/

ALTER TRIGGER IGI_CEC_GL_BC_PACKETS_T2 ENABLE;